#!/bin/bash

set -e
D="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

# This script builds a binary dpkg for Debian based distros.
# Original script from ripgrep - https://github.com/BurntSushi/ripgrep (licensed MIT)
#
# Adapted for the GoXLR Utility (RPM and DEB)
#
# Note that this requires both cargo-deb and cargo-generate-rpm which can be installed with
# 'cargo install cargo-deb cargo-generate-rpm'

if ! command -V cargo-deb > /dev/null 2>&1; then
    echo "cargo-deb command missing" >&2
    exit 1
fi

if ! command -V cargo-generate-rpm > /dev/null 2>&1; then
    echo "cargo-generate-rpm command missing" >&2
    exit 1
fi

# Neither 'cargo deb' or 'cargo generate-rpm' seem to provide a way to specify an asset that is created at build time.
# To work around this, we force a release build, copy out the shell completions produced from that build, put it into
# a predictable location and then build the packages, which knows where to look.
cargo build --release --all-features

strip target/release/goxlr-client
strip target/release/goxlr-daemon
strip target/release/goxlr-launcher
strip target/release/goxlr-defaults

DEPLOY_DIR=deployment/deb
OUT_DIR_CLIENT="$("$D"/cargo-out-dir target/release/ client-stamp)"
OUT_DIR_DAEMON="$("$D"/cargo-out-dir target/release/ daemon-stamp)"
mkdir -p "$DEPLOY_DIR"

# Copy Shell completions.
cp "$OUT_DIR_DAEMON"/{goxlr-daemon.bash,goxlr-daemon.fish,_goxlr-daemon} "$DEPLOY_DIR/"
cp "$OUT_DIR_CLIENT"/{goxlr-client.bash,goxlr-client.fish,_goxlr-client} "$DEPLOY_DIR/"

# Now, first build the .deb file (We don't need to recompile the code here)..
cargo deb -p goxlr-daemon --no-build

# Then the RPM
cargo generate-rpm -p daemon
cargo generate-rpm -p daemon --variant suse
